//
//  DWJStatusOriginalView.swift
//  weibo
//
//  Created by 代伟佳 on 16/4/21.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit
import SnapKit

class DWJStatusOriginalView: UIView {

    // 记录原创微博底部的约束
    var originalViewBottomConstraint: Constraint?

    
    var status: DWJStatus? {
    
        didSet{

            // 头像
            headImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))

            // 昵称
            nameLabel.text = status?.user?.screen_name
            // 会员
            mbrankImageView.image = getmbrankImage(status?.user?.mbrank ?? 0)
            
            // 时间
            timeLabel.text = "时间: 12-10"
            
            // 来自
            sourceLabel.text = "河北石家庄微博"

            // 认证
            verifiedImageView.image = getverifiedImage(status?.user?.verified_type ?? -1)
            
            // 微博正文
            contentLabel.text = status?.text
            

            
            // 先卸载
            self.originalViewBottomConstraint?.uninstall()
 
            // 是否有配图
            if status?.pic_urls?.count != 0 {
                
                
                // 给配图设置数据
                pictureView.pictureInfo = status?.pic_urls
                
                // 有配图 原创微博的底部等于配图的底部+10 显示配图
                self.snp_makeConstraints(closure: { (make) -> Void in
                   
                    self.originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint

                })
                pictureView.hidden = false

            
            } else {
                
                // 没配图 原创微博的底部等于微博正文底部+10 隐藏配图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(10).constraint
                })

                pictureView.hidden = true


            }
        }
    }
    
    // 通过服务器返回的int数据创建一个UIIMageView 会员
    func getmbrankImage(mbrank: Int) -> UIImage?{
    
        if mbrank > 0 && mbrank < 7 {
        
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        } else {
        
            return UIImage(named: "common_icon_membership_expired")
        }
    }
    
    // 认证图片逻辑
    func getverifiedImage(verified: Int) -> UIImage? {
    
        switch verified{
        
        case 0:
            return UIImage(named: "avatar_vip")
            
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
            
        case 220:
            return UIImage(named: "avatar_grassroot")
            
        default:
            return nil
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
    
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // MARK: - 设置视图
    private func setUpUI() {
        
        self.backgroundColor = UIColor.whiteColor()
    
        // 添加控件 
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            
//            make.top.equalTo(10)
//            make.left.equalTo(10)
            make.top.left.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(10)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in

            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.left.equalTo(headImageView)
            make.width.equalTo(kWIDTH - 20)
        }
        
        // 图片约束
        pictureView.snp_makeConstraints { (make) -> Void in
            
            // 该逻辑已经在pictureView里面写了
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo(contentLabel)
        }
        
        
        // 给原创微博设置约束
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
            
        }
    }
    
    
    // MARK: - 懒加载控件
    
    // 用户头像
    private lazy var headImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        
        return imageView
    }()
    
    // 用户昵称
    private lazy var nameLabel: UILabel = {
       
        let lab = UILabel(textColor: UIColor.darkGrayColor(), textFont: 14)
        
        return lab
    }()

    // 会员等级
    private lazy var mbrankImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "common_icon_membership"))
        
        return imageView
    }()
    
    // 时间
    private lazy var timeLabel: UILabel = {
    
        let lab = UILabel(textColor: ThemeColor, textFont: 10)
        
        return lab
    }()

    // 来自
    private lazy var sourceLabel: UILabel = {
        
        let lab = UILabel(textColor: ThemeColor, textFont: 10)
        
        return lab
    }()

    // 认证
    private lazy var verifiedImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "avatar_vgirl"))
        
        return imageView
    }()

    // 微博正文
    private lazy var contentLabel: UILabel = {
    
        let lab = UILabel(textColor: UIColor.blackColor(), textFont: 14)
        lab.numberOfLines = 0
        
        return lab
    }()
    
    // 微博配图
    private lazy var pictureView: DWJStatusPictureView = {
    
        let v = DWJStatusPictureView()
        
        return v
    }()
    

}
